package red.social;

import java.io.DataInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.app.Dialog;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class PerfilAmigoEncontrado extends Activity {
	Bundle parametros;
	String ip;
	String idamigo;
	int idusuario;
	Dialog dialog;
	EditText textoMensaje;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.perfilamigoencontrado);

		parametros = getIntent().getExtras();
		idusuario = parametros.getInt("idusuario");
		idamigo = parametros.getString("idamigo");
		ip = getResources().getString(R.string.ip);
		dialog = new Dialog(PerfilAmigoEncontrado.this);

		dialog.setCanceledOnTouchOutside(true);

		dialog.setTitle("Escribe el mensaje");

		dialog.setContentView(R.layout.dialogo_enviar_mensaje);

		Button enviar_dialogo = (Button) dialog
				.findViewById(R.id.enviarDialogo);

		textoMensaje = (EditText) dialog.findViewById(R.id.editText1);

		enviar_dialogo.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				try {

					HttpClient httpClient = new DefaultHttpClient();
					HttpPost postRequest = new HttpPost("http://" + ip
							+ ":8080/Modulo10-war/enviarMensaje");

					MultipartEntity reqEntity = new MultipartEntity(
							HttpMultipartMode.BROWSER_COMPATIBLE);
					reqEntity.addPart("texto", new StringBody(textoMensaje
							.getText().toString())); // IDUSUARIO
					reqEntity.addPart("emisor",
							new StringBody(String.valueOf(idusuario)));
					reqEntity.addPart("receptor", new StringBody(idamigo));
					postRequest.setEntity(reqEntity);
					httpClient.execute(postRequest);

				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				dialog.cancel();
			}
		});

		try {
			String servletURL = "http://" + ip + ":8080/Modulo10-war/InfoAmigo";

			URL ob = new URL(servletURL + "?idamigo=" + idamigo);
			URLConnection conn = ob.openConnection();
			conn.setDoInput(true);
			conn.setDoOutput(true);
			InputStream in = conn.getInputStream();
			ObjectInputStream dos = new ObjectInputStream(in);

			TextView nombre = (TextView) findViewById(R.id.nombreUsuario);
			nombre.setText((String) dos.readObject() + " "
					+ (String) dos.readObject());
			TextView email = (TextView) findViewById(R.id.textEmail);
			email.setText((String) dos.readObject());
			TextView estado = (TextView) findViewById(R.id.textEstado);
			estado.setText((String) dos.readObject());

			byte[] bImagen = (byte[]) dos.readObject();

			ImageView foto = (ImageView) findViewById(R.id.foto);

			foto.setImageBitmap(BitmapFactory.decodeByteArray(bImagen, 0,
					bImagen.length));

			Button boton1 = (Button) findViewById(R.id.buttonSolAmistad);
			boton1.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {

					try {
						String servletURL = "http://" + ip
								+ ":8080/Modulo10-war/EnvioAmistad";

						URL ob = new URL(servletURL + "?idamigo=" + idamigo
								+ "&idusuario=" + idusuario);
						URLConnection conn = ob.openConnection();
						conn.setDoInput(true);
						conn.setDoOutput(true);
						InputStream in = conn.getInputStream();
						DataInputStream dos = new DataInputStream(in);

						String leido = dos.readLine();
						if (leido.equals("0")) {
							CharSequence text = "Ya has enviado una solicitud a esta persona!";
							int duration = Toast.LENGTH_LONG;
							Toast toast = Toast.makeText(
									getApplicationContext(), text, duration);
							toast.show();
						} else if (leido.equals("1")) {
							CharSequence text = "Solicitud de Amistad enviada correctamente!";
							int duration = Toast.LENGTH_LONG;
							Toast toast = Toast.makeText(
									getApplicationContext(), text, duration);
							toast.show();
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});

			Button boton2 = (Button) findViewById(R.id.buttonEnviarMensaje);
			boton2.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {

					dialog.show();

				}
			});

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
